<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="com.beyondus.sso.utils.AesUtil" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.company.DepartmentComInfo" %>
<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.InputStreamReader" %>
<%@ page import="java.io.OutputStreamWriter" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.net.URLConnection" %>
<%@ page import="weaver.general.BaseBean" %>
<%
    JSONObject jsonData = new JSONObject();
    String type = Util.null2String( request.getParameter( "type" ) );
    String result = "";
    try {
        DepartmentComInfo comInfo = new DepartmentComInfo();
        if ("0".equals( type )) {//创建用户
            String lastname = Util.null2String( request.getParameter( "lastname" ) );
            String mobile = Util.null2String( request.getParameter( "mobile" ) );
            String password = Util.null2String( request.getParameter( "password" ) );
            String departmentid = Util.null2String( request.getParameter( "departmentid" ) );
            String departmentname = comInfo.getDepartmentName( departmentid );
            String loginid = Util.null2String( request.getParameter( "loginid" ) );
            JSONObject params = new JSONObject();
            params.put( "userName", lastname );
            params.put( "mobile", mobile );
            params.put( "password", password );
            params.put( "realName", lastname );
            params.put( "idCard", "" );
            params.put( "subUserId", loginid );
            params.put( "orgName", departmentname );
            bb.writeLog("params",params);
            result = addUser( params.toJSONString() );
        }
        if ("1".equals( type )) {//删除用户
            String loginid = Util.null2String( request.getParameter( "loginid" ) );
            JSONObject params = new JSONObject();
            params.put( "subUserId", loginid );
            bb.writeLog("params",params);
            result = delUser(params.toJSONString());
        }
        bb.writeLog( "result" + result);
    } catch (Exception e) {
        e.printStackTrace();
    }
    jsonData.put("result",result);
    out.print( jsonData.toJSONString() );
    bb.writeLog( "getUserInfo_Ajax.jsp=" + jsonData.toJSONString() );
%>
<%!
    public static final String appId = "rCFQAcs9MgIj9Rjy";
    public static final String appKey = "ede518c4811d4a31b32ade7622e610e0";
    //新增用户
    public static String addUser(String paramsStr) throws Exception {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>>addUser>>>paramsStr="+paramsStr);
        String url = "http://sso.zealdee.com.cn/sso/create";
        JSONObject params = new JSONObject();
        String encrypt = AesUtil.aesEncrypt( appKey, paramsStr );
        params.put( "appId", appId );
        params.put( "ciphertext", encrypt );
        baseBean.writeLog(">>>>>>addUser>>>params="+params);
        String result = doPostSoap( url, params.toJSONString() );
        baseBean.writeLog(">>>>>>addUser>>>result="+result);
        return result;
    }
    //删除用户
    public static String delUser(String paramsStr) throws Exception {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog(">>>>>>delUser>>>paramsStr="+paramsStr);
        String url = "http://sso.zealdee.com.cn/sso/del";
        JSONObject params = new JSONObject();
        String encrypt = AesUtil.aesEncrypt( appKey, paramsStr );
        params.put( "appId", appId );
        params.put( "ciphertext", encrypt );
        baseBean.writeLog(">>>>>>delUser>>>params="+params);
        String result = doPostSoap( url, params.toJSONString() );
        baseBean.writeLog(">>>>>>delUser>>>result="+result);
        return result;
    }

    /***
     *
     * @param url
     * @param params
     * @return
     * @throws Exception
     */
    public static String doPostSoap(String url, String params) {
        OutputStreamWriter outputStreamWriter = null;
        BufferedReader bufferedReader = null;
        String result = "";
        try {
            URL realUrl = new URL( url );
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty( "Content-Type", "application/json;charset=UTF-8" );
            // 发送POST请求必须设置如下两行
            connection.setDoOutput( true );
            connection.setDoInput( true );
            //1.获取URLConnection对象对应的输出流
            //out = new PrintWriter(connection.getOutputStream());
            //2.中文有乱码的需要将PrintWriter改为如下
            outputStreamWriter = new OutputStreamWriter( connection.getOutputStream(), "UTF-8" );
            // 发送请求参数
            outputStreamWriter.write( params );
            // flush输出流的缓冲
            outputStreamWriter.flush();
            // 定义BufferedReader输入流来读取URL的响应
            bufferedReader = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //使用finally块来关闭输出流、输入流
        finally {
            try {
                if (outputStreamWriter != null) {
                    outputStreamWriter.close();
                }
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

%>